package com.sx.shopping.controller.goods;

import java.math.BigDecimal;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.model.manage.mybatis.vo.bank.MDropDownVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultDropDownListVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
import com.sx.model.manage.mybatis.vo.result.ResultUploadVO;
import com.sx.model.shopping.mybatis.form.goods.MGoodsInfoForm;
import com.sx.model.shopping.mybatis.vo.goods.MGoodsInfoVO;
import com.sx.shopping.service.goods.MGoodsInfoWebService;



@Controller
@RequestMapping("/goods")
public class MGoodsController extends MBaseController{
	/**
	 * 日志
	 */
	private Logger _log = LoggerFactory.getLogger(this.getClass());
	@Resource(name="goodsInfoWebService")
	private MGoodsInfoWebService goodsInfoWebServiceImpl ;
	/**
	   * 跳转到数据列表页
	   */
	  @RequestMapping(value="goodManageList")
	  public ViewAndModel pageIndex(){
		  ViewAndModel model=new ViewAndModel(request, cache );
		  				  model.setViewName("goodsManage/goodsManageList");
	
		  return model;
	  }
	/**
	 *   保存
	 * @param form
	 * @return
	 */
	@RequestMapping(value="save")
	@ResponseBody
	public  ResultBooleanVO  save(MGoodsInfoForm  form ,BindingResult errors){
		_log.debug("save调用正常");
		if (null == form) {
			return new ResultBooleanVO("m.parameter.is.null");
		}
		if (null == form.getPrice()) {
			return new ResultBooleanVO(false, null, "金额格式不正确！");
		}
		Boolean b = is_num(String.valueOf(form.getPrice()));
		if (!b) {
			return new ResultBooleanVO(false, null, "金额格式不正确！");
		}
		return goodsInfoWebServiceImpl.save(form, this.loginConsumer());		
	}
	/**
	 *  取消 
	 * @param form
	 * @return
	 */
	@RequestMapping(value="cancel")
	@ResponseBody
	public  ResultBooleanVO  cancel(MGoodsInfoForm  form ){
		if(  null == form){
			return  new ResultBooleanVO(false,null,"任何没有传过来");
		}
		_log.debug("cancel调用正常");			
		return new ResultBooleanVO();
	}
	/**
	 *  保存并发布
	 * @param form
	 * @return
	 */
	@RequestMapping(value="saveAndIssue")
	@ResponseBody
	public  ResultBooleanVO  saveAndIssue(MGoodsInfoForm  form ,BindingResult errors){

		if(  null == form){
			return  new ResultBooleanVO(false,null,"任何没有传过来");
		}
		if (null == form.getPrice()) {
			return new ResultBooleanVO(false, null, "金额格式不正确！");
		}
		Boolean b = is_num(String.valueOf(form.getPrice()));
		if (!b) {
			return new ResultBooleanVO(false, null, "金额格式不正确！");
		}
		_log.debug("save调用正常");
		return goodsInfoWebServiceImpl.saveAndIssue(form, this.loginConsumer());
	}
	/**
	   * 跳转到 添加界面
	   */
	  @RequestMapping(value="toAdd")
	  public ViewAndModel toAdd(){
		  ViewAndModel model=new ViewAndModel(request, cache );
		  				  model.setViewName("goodsManage/addGoods");
	
		  return model;
	  }
	  /**
	   * 跳转到编辑界面
	   */
	  @RequestMapping(value="toEdit/{id}")
	  public ViewAndModel toEdit(@PathVariable Long id ){
		  ViewAndModel model=new ViewAndModel(request, cache );
		  				  model.setViewName("goodsManage/editGoods");
		  ResultObjectVO<MGoodsInfoVO> result = goodsInfoWebServiceImpl.queryById(id);
		 		  		
		  model.addObject("goods",  result.getObjVo());
		  return model;
	  }
	  /**
	   * 跳转到详情界面
	   */
	  @RequestMapping(value="toView/{id}")
	  public ViewAndModel toView(@PathVariable Long id ){
		  ViewAndModel model=new ViewAndModel(request, cache );
		  				  model.setViewName("goodsManage/goodsDetails");
		  ResultObjectVO<MGoodsInfoVO> result = goodsInfoWebServiceImpl.queryById(id);
		  		
		  model.addObject("goods",  result.getObjVo());
		  return model;
	  }
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	@RequestMapping(value="queryById")
	@ResponseBody
	public  ResultObjectVO<MGoodsInfoVO> queryById(Long id){
		if( id == null){
			return  new ResultObjectVO<>(false,null,"id没有传过来");
		}	
		return goodsInfoWebServiceImpl.queryById(id);
	}
	
	/**
	 * 查询分页信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="queryPage")
	@ResponseBody
	public ResultListVO<MGoodsInfoVO>  queryPage( MGoodsInfoForm  form ){ 
		_log.debug("商品queryPage调用正常");
		return this.goodsInfoWebServiceImpl.queryPage(form);
	}
	
	
	/** 6 商品上架 */ 
	/**
	 * 查询分页信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="onLine/{id}")
	@ResponseBody
	public ResultBooleanVO  onLine(@PathVariable Long id ){
		_log.debug("onLine调用正常");
		return this.goodsInfoWebServiceImpl.onLine( new Long[]{id},this.loginConsumer());
	} 
	
	/**  */ 
	/**
	 * 6.1 商品下架
	 * @param id
	 * @return
	 */
	@RequestMapping(value="offLine/{id}")
	@ResponseBody
	public ResultBooleanVO  offLine(@PathVariable Long id ){ 
		_log.debug("offLine调用正常");
		return this.goodsInfoWebServiceImpl.offLine( new Long[]{id},this.loginConsumer());
	} 
	

	/**
	 * 7 根据or条件获取唯一数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value="queryUniqueByOr")
	@ResponseBody
	public ResultObjectVO<MGoodsInfoVO> queryUniqueByOr( MGoodsInfoForm  form ){ 
		return this.goodsInfoWebServiceImpl.queryUniqueByOr(form);
	}	
	
	/**
	 * 8 根据 or 条件获取数据集合
	 * @param id
	 * @return
	 */
	@RequestMapping(value="queryListByOr")
	@ResponseBody
	public ResultListVO<MGoodsInfoVO> queryListByOr( MGoodsInfoForm  form ){ 
		return this.goodsInfoWebServiceImpl.queryListByOr(form);
	}

	/**
	 * 8.1 获取数据集合
	 * @param id
	 * @return
	 */
	@RequestMapping(value="queryList")
	@ResponseBody
	public ResultListVO<MGoodsInfoVO> queryList( MGoodsInfoForm  form ){ 
				return this.goodsInfoWebServiceImpl.queryList(form);
	}

	/**
	 * 9 更新商品价格
	 * @param id
	 * @return
	 */
	@RequestMapping(value="updateGoodPrice")
	@ResponseBody
	public ResultBooleanVO updateGoodPrice(BigDecimal price,Long id){ 		
		if( null == price){
			_log.debug("price没传过来");
			return new ResultBooleanVO(false,null,"price没传过来");
		}
		if( null == id){
			_log.debug("id没传过来");
			return new ResultBooleanVO(false,null,"id没传过来");
		}
		_log.debug("updateGoodPrice调用正常");
		return this.goodsInfoWebServiceImpl.updateGoodPrice(price,id,this.loginConsumer());
	}
	
	/**
	 * 10 更新商品图片信息 localimage
	 * @param id
	 * @return
	 */
	@RequestMapping(value="updateGoodLocalimage")
	@ResponseBody
	public ResultBooleanVO updateGoodLocalimage(String local_image,Long id){
		if(StringUtils.isBlank(local_image)){
			_log.debug("local_image没传过来");
			return new ResultBooleanVO(false,null,"local_image没传过来");
		}
		if( null == id){
			_log.debug("id没传过来");
			return new ResultBooleanVO(false,null,"id没传过来");
		}
		_log.debug("updateGoodLocalimage调用正常");
		return this.goodsInfoWebServiceImpl.updateGoodLocalimage(local_image,id,this.loginConsumer());
	}
	
	/**
	 * 11 上传商品图片
	 * @param file
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/upload")
	@ResponseBody
	public ResultUploadVO upload(@RequestParam(value = "files", required = false) MultipartFile file, HttpServletRequest request) { 
		if( null == file){
			_log.debug("file没传过来");
			return new ResultUploadVO(false,null,"file没传过来");
		}

		_log.debug("upload调用正常");
		return this.goodsInfoWebServiceImpl.upload(file, request,this.loginConsumer());
	}
	/**
	 * 12 获取数据集合
	 * @param id
	 * @return
	 */
	@RequestMapping(value="queryListForDropDown")
	@ResponseBody
	public ResultDropDownListVO<MDropDownVO> queryListForDropDown( MGoodsInfoForm  form ){ 
				return this.goodsInfoWebServiceImpl.queryListForDropDown(form);
	}
}
